package org.example.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.models.auth.In;
import org.example.entity.Question;
import org.example.entity.TestResult;
import org.example.entity.Tester;
import org.example.entity.TesterVo;
import org.example.pojo.Result;
import org.example.service.IQuestionService;
import org.example.service.ITestResultService;
import org.example.service.ITesterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/tester")
public class TesterController {
    @Autowired
    private ITesterService iTesterService;

    @Autowired
    private IQuestionService iQuestionService;

    @Autowired
    private ITestResultService iTestResultService;

    //测试者登录的界面
    @RequestMapping("/login")
    @ResponseBody
    public Result login(Tester tester){
        LambdaQueryWrapper<Tester> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Tester::getPhone,tester.getPhone());
        List<Tester> testerList = iTesterService.list(lambdaQueryWrapper);
        if(testerList.size() == 0){
            //设置做题时间
            tester.setCreateTime(new Date());
            iTesterService.save(tester);
            return new Result(0,"还没测试过",1,tester);
        }
        return new Result(1,"已经测试过",1,testerList.get(0));
    }
    //跳转到做题界面
    @RequestMapping("/listQuestion")
    public String listQuestion(Model model,Integer testerId){
        List<Question> questionList = iQuestionService.list();
        model.addAttribute("questionList",questionList);
        model.addAttribute("testerId",testerId);
        return "test_question";

    }
    //保存测试结果
    @RequestMapping("/saveResult")
    @ResponseBody
    public Result saveResult(@RequestBody List<TestResult> testResults){
        iTestResultService.saveBatch(testResults);
        LambdaUpdateWrapper<Tester> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Tester::getStatus,1);
        updateWrapper.eq(Tester::getId,testResults.get(0).getTesterId());
        iTesterService.update(updateWrapper);
        return new Result(0,"成功",1,testResults.get(0).getTesterId());
    }

    //跳转到测试结果界面
    @RequestMapping("/show")
    public String show(Integer testerId,Model model){
        model.addAttribute("testerId",testerId);
        return "test_show";
    }

    //获取测试结果
    @RequestMapping("/testerData")
    @ResponseBody
    public Result testerData(Integer testerId){
        TesterVo testerVo = iTesterService.getTesterVoById(testerId);
        return new Result(0,"success",1,testerVo);
    }

    //获取所有测试者信息
    @RequestMapping("/listTesterVo")
    @ResponseBody
    public Result listTesterVo(Integer page,Integer limit,String searchTesterName){
        PageInfo<TesterVo> testVoListByPage = iTesterService.getTestVoListByPage(page, limit,searchTesterName);
        return new Result<>(0,"success",testVoListByPage.getTotal(),testVoListByPage.getList());
    }

}
